package com.example.controller;

import com.example.entity.Repair;
import com.example.entity.Stayinfo;
import com.example.mapper.RepairMapper;
import com.example.mapper.StayinfoMapper;
import com.example.service.RepairService;
import com.example.service.StayinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/Repair")
public class RepairConstoller {

    @Autowired
    private RepairMapper repairMapper;
    @Autowired
    private RepairService repairService;
    //修改和更新数据
    @PostMapping
    public Map<String, Object> save(@RequestBody Repair repair){
        return repairService.saveRepair(repair);
    }
    //展示所有数据
    @GetMapping
    public List<Repair> index() {
        List<Repair> all = repairMapper.finAll();
        return all;
    }

    //根据学号删除数据
    @DeleteMapping("/delete")
    public Map<String, Object> deleteBy(@RequestParam String repair_num) {
        return repairService.deleteByRepairNum(repair_num);
    }
    @GetMapping("/page")
    public Map<String, Object> findPageWithOptionalParams(@RequestParam(required = false) Integer pageNum,
                                                          @RequestParam(required = false) Integer pageSize,
                                                          @RequestParam(required = false) String floor_num) {
        pageNum = (pageNum - 1) * pageSize;
        List<Repair> data = repairMapper.selectPage(pageNum, pageSize,floor_num);
        Integer total = repairMapper.selectTotal(floor_num);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

    @GetMapping("/pages")
    public Map<String, Object> findPageWithOptionalParams1(@RequestParam(required = false) Integer pageNum,
                                                          @RequestParam(required = false) Integer pageSize,
                                                          @RequestParam(required = false) String stu_num) {
        pageNum = (pageNum - 1) * pageSize;
        List<Repair> data = repairMapper.selectPage1(pageNum, pageSize,stu_num);
        Integer total = repairMapper.selectTotal1(stu_num);
        Map<String, Object> res = new HashMap<>();
        res.put("data", data);
        res.put("total", total);
        return res;
    }

}
